package com.example.onlinemusic.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.onlinemusic.common.R;
import com.example.onlinemusic.model.domain.Consumer;
import com.example.onlinemusic.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户管理控制类
 */
@RestController
public class ConsumerController {

    @Autowired
    ConsumerService consumerService;

    /**
     * 获取所有客户信息
     * @param curPage 当前页
     * @param pageSize 每页数量
     * @return
     */
    @GetMapping("/user")
    public R allUser(@RequestParam(defaultValue = "1") int curPage,@RequestParam(defaultValue = "1") int pageSize) {
        // 分页查询数据
        IPage<Consumer> page = new Page<Consumer>();
        page.setCurrent(curPage);
        page.setSize(pageSize);
        return consumerService.allUser(page);
    }

    /**
     * 根据id删除用户信息
     *
     * @param id
     * @return
     */
    @GetMapping("/user/delete")
    public R deleteUser(@RequestParam int id) {
        return consumerService.deleteUser(id);
    }

    /**
     * 删除多个用户
     *
     * @param id
     * @return
     */
    @DeleteMapping("/user/deleteIds")
    public R deleteUsers(@RequestParam String[] id) {
        return consumerService.deleteByIds(id);
    }

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    // http://localhost:8888/user/detail?id=1
    @GetMapping("/user/detail")
    public R userDetail(@RequestParam int id){
        return consumerService.userDetail(id);
    }


    /**
     * 首页模块查询用户数据
     * @return
     */
    @GetMapping("/users")
    public R allUser(){
        return consumerService.allUser();
    }

}
